package com.luhuiguo.gwt.mvp.dispatcher.server.spring;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.luhuiguo.gwt.mvp.dispatcher.server.AbstractDispatcherServlet;
import com.luhuiguo.gwt.mvp.dispatcher.server.Dispatcher;


public class SpringDispatcherServlet extends AbstractDispatcherServlet {
    
    private static final long serialVersionUID = 8494611763356107295L;

    private Dispatcher dispatcher;
    
    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        WebApplicationContext ctx = WebApplicationContextUtils
                .getRequiredWebApplicationContext(config.getServletContext());
        AutowireCapableBeanFactory beanFactory = ctx
                .getAutowireCapableBeanFactory();
        beanFactory.autowireBean(this);
    }
 

    @Override
    protected Dispatcher getDispatcher() {
        return dispatcher;
    }
    
    @Autowired
    @Required
    public void setDispatcher(Dispatcher dispatcher) {
        this.dispatcher = dispatcher;
    }

}
